<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件列表</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: #f1f5f9;
            min-height: 100vh;
        }

        .header {
            background: #1e3a8a;
            color: white;
            padding: 1rem 2rem;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .header-content {
            display: flex;
            justify-content: space-between;
            align-items: center;
            max-width: 1200px;
            margin: 0 auto;
        }

        .header h1 {
            font-size: 1.5rem;
            color: white;
            font-weight: 600;
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 1rem;
        }

        .login-btn {
            background: rgba(255, 255, 255, 0.2);
            color: white;
            border: 1px solid rgba(255, 255, 255, 0.3);
            padding: 0.5rem 1rem;
            border-radius: 6px;
            text-decoration: none;
            font-weight: 500;
            transition: background 0.3s ease;
        }

        .login-btn:hover {
            background: rgba(255, 255, 255, 0.3);
        }

        .main-container {
            max-width: 1200px;
            margin: 2rem auto;
            padding: 0 2rem;
        }

        .search-section {
            background: white;
            padding: 1.5rem;
            border-radius: 8px 8px 0 0;
            border-bottom: 1px solid #e1e5e9;
        }

        .search-box {
            display: flex;
            gap: 1rem;
            align-items: center;
        }

        .search-input {
            flex: 1;
            padding: 0.75rem;
            border: 2px solid #e1e5e9;
            border-radius: 6px;
            font-size: 1rem;
            outline: none;
        }

        .search-input:focus {
            border-color: #1e3a8a;
        }

        .search-btn {
            background: #1e3a8a;
            color: white;
            border: none;
            padding: 0.75rem 1.5rem;
            border-radius: 6px;
            cursor: pointer;
            font-weight: 500;
            transition: background 0.3s ease;
        }

        .search-btn:hover {
            background: #1e40af;
        }

        .files-section {
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            overflow: hidden;
        }

        .files-header {
            background: #f8f9fa;
            padding: 1rem 1.5rem;
            border-bottom: 1px solid #e1e5e9;
            text-align: right;
        }

        .files-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 1rem;
            table-layout: fixed;
        }

        .files-table th,
        .files-table td {
            padding: 1rem;
            text-align: center;
            border-bottom: 1px solid #eee;
            white-space: nowrap;
            overflow: hidden;
        }

        .files-table td:first-child {
            text-align: center;
        }

        .files-table th:nth-child(1) { width: 33.33%; }
        .files-table th:nth-child(2) { width: 33.33%; }
        .files-table th:nth-child(3) { width: 33.33%; }

        .files-table th {
            background: #f8f9fa;
            font-weight: 600;
            color: #333;
        }

        .files-table tr:hover {
            background: #f8f9fa;
        }

        .file-name {
            color: #1e3a8a;
            cursor: pointer;
            text-decoration: none;
            font-weight: 500;
            font-size: 1.1rem;
            transition: color 0.3s ease;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            display: inline-block;
            max-width: 100%;
            width: 100%;
        }

        .file-name:hover {
            color: #1e40af;
            text-decoration: underline;
        }

        .file-size {
            color: #666;
        }

        .file-time {
            color: #999;
            font-size: 0.9rem;
        }

        .pagination {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-top: 2rem;
            padding: 1rem;
            background: #f8f9fa;
            border-top: 1px solid #e1e5e9;
        }

        .pagination-info {
            color: #666;
            font-size: 0.9rem;
        }

        .pagination-buttons {
            display: flex;
            gap: 0.5rem;
            align-items: center;
        }

        .page-btn {
            background: white;
            border: 1px solid #e1e5e9;
            color: #333;
            padding: 0.5rem 1rem;
            border-radius: 6px;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .page-btn:hover {
            background: #f8f9fa;
            border-color: #1e3a8a;
        }

        .page-btn.active {
            background: #1e3a8a;
            color: white;
            border-color: #1e3a8a;
        }

        .page-btn:disabled {
            background: #f5f5f5;
            color: #ccc;
            cursor: not-allowed;
            border-color: #e1e5e9;
        }

        .empty-state {
            text-align: center;
            padding: 3rem;
            color: #666;
        }

        .empty-state .icon {
            font-size: 4rem;
            color: #ccc;
            margin-bottom: 1rem;
        }

        .loading {
            text-align: center;
            padding: 2rem;
            color: #666;
        }



        @media (max-width: 768px) {
            .main-container {
                padding: 0 1rem;
            }
            
            .search-box {
                flex-direction: column;
                align-items: stretch;
            }
            
            .pagination {
                flex-direction: column;
                gap: 1rem;
                text-align: center;
            }
            
            .files-table {
                font-size: 0.9rem;
            }
            
            .files-table th,
            .files-table td {
                padding: 0.5rem;
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="header-content">
            <h1>文件列表</h1>
            <div class="user-info">
                <a href="/admin" class="login-btn">管理员登录</a>
            </div>
        </div>
    </div>

    <div class="main-container">
        <div class="files-section">
            <!-- 搜索区域 -->
            <div class="search-section">
                <div class="search-box">
                    <input type="text" id="searchInput" class="search-input" placeholder="搜索文件名..." value="">
                    <button class="search-btn" onclick="searchFiles()">搜索</button>
                    <button onclick="clearSearch()" style="background: #1e3a8a; color: white; border: none; padding: 0.75rem 1.5rem; border-radius: 6px; cursor: pointer; font-weight: 500;">
                        清除
                    </button>
                </div>
            </div>
            
            <div id="filesContainer">
                <div class="loading">加载中...</div>
            </div>
        </div>
        

    </div>

    <script>
        let currentPage = 1;
        let currentSearch = '';
        let currentPagination = null;

        // 页面加载时获取文件列表
        document.addEventListener('DOMContentLoaded', function() {
            loadPublicFiles();
            
            // 搜索框回车键支持
            document.getElementById('searchInput').addEventListener('keypress', function(e) {
                if (e.key === 'Enter') {
                    searchFiles();
                }
            });
        });

        // 搜索文件
        function searchFiles() {
            currentSearch = document.getElementById('searchInput').value.trim();
            currentPage = 1;
            loadPublicFiles();
        }

        // 清除搜索
        function clearSearch() {
            document.getElementById('searchInput').value = '';
            currentSearch = '';
            currentPage = 1;
            loadPublicFiles();
        }

        // 加载公开文件列表
        async function loadPublicFiles() {
            try {
                const params = new URLSearchParams({
                    search: currentSearch,
                    page: currentPage,
                    limit: 10
                });
                
                const response = await fetch(`/public/files?${params}`);
                if (response.ok) {
                    const data = await response.json();
                    currentPagination = data.pagination;
                    displayPublicFiles(data.files);
                    displayPagination();
                } else {
                    showError('加载文件列表失败');
                }
            } catch (error) {
                console.error('加载文件失败:', error);
                showError('网络错误，请重试');
            }
        }

        // 显示公开文件列表
        function displayPublicFiles(files) {
            const container = document.getElementById('filesContainer');
            
            if (files.length === 0) {
                container.innerHTML = `
                    <div class="empty-state">
                        <div class="icon">📁</div>
                        <div>${currentSearch ? '没有找到匹配的文件' : '暂无文件'}</div>
                        <div style="font-size: 0.9rem; margin-top: 0.5rem;">
                            ${currentSearch ? '请尝试其他搜索关键词' : '管理员上传文件后即可在此查看'}
                        </div>
                    </div>
                `;
                return;
            }

                        const table = `
                <table class="files-table">
                    <thead>
                        <tr>
                            <th>文件名</th>
                            <th>大小</th>
                            <th>上传时间</th>
                        </tr>
                    </thead>
                    <tbody>
                        ${files.map(file => `
                            <tr>
                                <td>
                                    <a href="/public/download/${file.id}" class="file-name" download title="${file.filename}">
                                        ${file.filename}
                                    </a>
                                </td>
                                <td class="file-size">${formatFileSize(file.size || 0)}</td>
                                <td class="file-time">${formatDate(file.uploadTime)}</td>
                            </tr>
                        `).join('')}
                    </tbody>
                </table>
            `;
            
            container.innerHTML = table;
        }

        // 显示分页
        function displayPagination() {
            const paginationDiv = document.createElement('div');
            paginationDiv.className = 'pagination';
            
            // 文件数量信息
            let paginationInfo = '';
            if (currentPagination) {
                const { currentPage, totalPages, totalFiles } = currentPagination;
                const startIndex = (currentPage - 1) * 10 + 1;
                const endIndex = Math.min(currentPage * 10, totalFiles);
                paginationInfo = `第 ${startIndex}-${endIndex} 条，共 ${totalFiles} 条`;
            }
            
            // 分页按钮
            let paginationButtons = '';
            if (currentPagination && currentPagination.totalPages > 1) {
                const { currentPage, totalPages, hasPrev, hasNext } = currentPagination;
                
                // 上一页按钮
                paginationButtons += `
                    <button class="page-btn" onclick="changePage(${currentPage - 1})" ${!hasPrev ? 'disabled' : ''}>
                        上一页
                    </button>
                `;
                
                // 页码按钮
                for (let i = 1; i <= totalPages; i++) {
                    if (i === 1 || i === totalPages || (i >= currentPage - 2 && i <= currentPage + 2)) {
                        paginationButtons += `
                            <button class="page-btn ${i === currentPage ? 'active' : ''}" onclick="changePage(${i})">
                                ${i}
                            </button>
                        `;
                    } else if (i === currentPage - 3 || i === currentPage + 3) {
                        paginationButtons += '<span class="page-btn" style="border: none; background: none;">...</span>';
                    }
                }
                
                // 下一页按钮
                paginationButtons += `
                    <button class="page-btn" onclick="changePage(${currentPage + 1})" ${!hasNext ? 'disabled' : ''}>
                        下一页
                    </button>
                `;
            }
            
            paginationDiv.innerHTML = `
                <div class="pagination-info">${paginationInfo}</div>
                <div class="pagination-buttons">${paginationButtons}</div>
            `;
            
            // 移除旧的分页，添加新的
            const oldPagination = document.querySelector('.pagination');
            if (oldPagination) {
                oldPagination.remove();
            }
            
            document.querySelector('.files-section').appendChild(paginationDiv);
        }

        // 切换页面
        function changePage(page) {
            currentPage = page;
            loadPublicFiles();
        }

        // 显示错误信息
        function showError(message) {
            const container = document.getElementById('filesContainer');
            container.innerHTML = `
                <div class="empty-state">
                    <div class="icon">❌</div>
                    <div>${message}</div>
                </div>
            `;
        }

        // 格式化文件大小
        function formatFileSize(bytes) {
            if (bytes === 0) return '0 Bytes';
            const k = 1024;
            const sizes = ['Bytes', 'KB', 'MB', 'GB'];
            const i = Math.floor(Math.log(bytes) / Math.log(k));
            return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
        }

        // 格式化日期
        function formatDate(dateString) {
            const date = new Date(dateString);
            return date.toLocaleString('zh-CN');
        }
    </script>
</body>
</html> 